<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/8
 * Time: 14:20
 */

namespace app\admin\controller;
use app\admin\common\Base;
use think\Exception;
use think\facade\Request;
use think\facade\Session;

class Article extends Base
{
    public function artList(){
        $condition = [];
        if (Session::get('is_admin') != 1){
            $condition[] = ['user_id', '=', Session::get('admin_id')];
        }
        $artList = \app\admin\model\Article::field('id,title,cate_id,user_id,pv,create_time')->where($condition)->order('id desc')->paginate(10);
        $this->assign('artList', $artList);
        $this->assign('empty', '<h3>暂时没有数据</h3>');
        return $this->view->fetch('artlist');
    }

    public function edit(){
        if (Request::isPost()){
            $data = Request::post();
            $id = isset($data['id'])?$data['id']:0;
            if ($_FILES['title_img']['name']){
                try{
                    $file = Request::file('title_img');
                }catch (Exception $e){
                    return $this->error($e->getMessage(), 'insert');
                }
                $info = $file->validate([
                    'size'=>'10240000',
                    'ext'=>'jpeg,jpg,png,gif'
                ])->move('uploads/');
                if ($file->getError()){
                    return $this->error($file->getError());
                }
                if ($id){
                    $title_img = \app\admin\model\Article::where('id',$id)->value('title_img');
                    if ($title_img && file_exists('./uploads/'.$title_img)){
                        unlink('./uploads/'.$title_img);
                    }
                }
                $data['title_img'] = $info->getSaveName();
            }

            if ($id){
                unset($data['id']);
                # 更新
                $res = \app\admin\model\Article::where('id',$id)->update($data);
            }else{
                $data['user_id'] = Session::get('admin_id');
                $data['status'] = 1;
                # 新增
                $res = \app\admin\model\Article::create($data);
            }
            if ($res){
                return $this->success('保存成功','artList');
            }
            return $this->error('保存失败，请稍后再试');
        }
        $id = Request::param('id');
        $article = null;
        if ($id){
            $article = \app\admin\model\Article::get($id);
            if (empty($article)){
                return $this->error('非法访问');
            }
        }
        $cateList = \app\admin\model\Cate::column('id,name');
        $this->assign('cateList', $cateList);
        $this->assign('artInfo', $article);
        return $this->view->fetch();
    }

    public function doDel(){
        $id = Request::param('id');
        if (empty($id)){
            return $this->error('非法访问');
        }
        $art = \app\admin\model\Article::get($id);
        if (empty($art)){
            return $this->error('非法访问');
        }
        if (file_exists('./uploads/'.$art->title_img)){
            unlink('./uploads/'.$art->title_img);
        }
        $res = $art->delete();
        if ($res){
            return $this->success('删除成功','artList');
        }
        return $this->error('删除失败，请稍后再试');
    }
}